package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.dao.core.UserToAgentDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.service.MachineLoseService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 丢失车辆
 *
 * @author: ShiGuang
 * @create: 2021-12-15 10:06
 */
@RestController
@RequestMapping("/machineLost")
public class MachineLostController {

    @Autowired
    private MachineLoseService machineLoseService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private UserToAgentDao userToAgentDao;


    /**
     * 根据中控编号判断周围车辆是否是失窃车辆
     *
     * @param token         令牌
     * @param machineNOList 车辆编号集合，用逗号分隔
     * @param lo            经度
     * @param la            纬度
     * @param mapType       地图类型
     * @param response      响应
     * @ignoreParams request
     */
    @RequestMapping("/getByMachineNO")
    public void getByMachineNO(String token, @RequestParam List<String> machineNOList, Double lo, Double la, Integer mapType, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<>();
        try {
            User user = tokenService.getUser(token);
            // 如果地图类型为空，默认谷歌
            if (mapType == null) {
                mapType = SysConstant.GOOGLE;
            }
            Assert.notNull(lo, "-2001", "经度不能为空");
            Assert.notNull(la, "-2002", "纬度不能为空");

            if (user != null) {
                // 发送信息
                machineLoseService.sendMessage(machineNOList, lo, la, mapType, user.getAccountId());
                map.put(ResponseCode.RET, ResponseCode.SUCC);
                map.put(ResponseCode.DATA, "SUCC");
            }
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }
        OutputUtil.renderText(request, response, map);
    }
}